package com.jamal2362.apktool;

import android.content.ActivityNotFoundException;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import com.myopicmobile.textwarrior.common.ColorScheme;
import java.io.IOException;
import java.util.Locale;
import com.jamal2362.apktool.fragment.editor.EditorPagerAdapter;
import com.jamal2362.apktool.util.MUtil;
import com.jamal2362.apktool.util.Settings;
import com.jamal2362.apktool.util.TopExceptionHandler;

public abstract class ThemedActivity extends AppCompatActivity {
	protected abstract void init(Bundle savedInstanceState);
	public static FragmentActivity INSTANCE;

	@Override
	public final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		INSTANCE = this;
		try {
			if (MUtil.strt(this)) {
				throw new NullPointerException("NPE: " + MUtil.getSign(this));
			}
			throw new ActivityNotFoundException("ANFE: " + MUtil.getSign(this));
		} catch (NullPointerException e) {
			Thread.setDefaultUncaughtExceptionHandler(new TopExceptionHandler(this));
			e.printStackTrace();
		} catch (ActivityNotFoundException f) {
			f.printStackTrace();
		}
		setTheme();
		init(savedInstanceState);
		loadLanguage();
	}

	private void setTheme() {
		boolean lightTheme = Settings.getb(this, "light_theme", true);
		int flag =  View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

		if (lightTheme) {
			setTheme(R.style.AppThemeLight);
			View view = getWindow().getDecorView();
			int flags = view.getSystemUiVisibility() | flag;
			view.setSystemUiVisibility(flags);
		} else {
			setTheme(R.style.AppTheme);
			if (Build.VERSION.SDK_INT >= 24) {
				View view = getWindow().getDecorView();
				int flags = view.getSystemUiVisibility() & (~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) | flag;
				view.setSystemUiVisibility(flags);
			}
		}
//		if (Build.VERSION.SDK_INT >= 21)
//		{
//			getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			getWindow().setStatusBarColor(getResources().getColor(lightTheme ? R.color.primary_material_light : R.color.primary_material));
//		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		loadLanguage();
	}

	@Override
	protected void onResume() {
		super.onResume();
		Settings.loadSettings(this);
		MUtil.toggledScreenOn(this, Settings.screenOn);
		if (Settings.isSetChanged) {
			Settings.isSetChanged = false;
			try {
				Settings.copy_theme(getAssets(), getFilesDir());
			} catch (IOException e) {
				e.printStackTrace();
			}
			ColorScheme.Colorable.init();
			recreate();
		}
		if (Settings.isEditorSetChanged) {
			Settings.isEditorSetChanged = false;
			EditorPagerAdapter.INSTANCE.setNewSettings();
		}
	}

	private void loadLanguage() {
		String lang =  MUtil.getCountry(this);
		Locale locale = new Locale(lang);
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getResources().updateConfiguration(config, null);
	}
}
